package com.steellee.leeexcel;

import com.steellee.leeexcel.dto.*;
import org.apache.poi.ss.usermodel.*;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

public class PaExcelWriteWithStyleDemo extends Parent {
    @Autowired
    private PaExcelWrite operate;

    /**
     * 入参数据对象类型， 多个 sheet数据写出
     *
     * @ throws Exception
     */
    @Test
    public void writeWithStyleBy0bjMoreSheet() throws Exception {
        List<MetaExcelWriteDTO> writes = new ArrayList<>();

        // sheet1 带自定义样式
        MetaExcelWriteDTO write = setStyle();
        get0bjData(write);
        writes.add(write);

        // sheet2 取消自定义样式
        write = setStyle();
        get0bjData(write);
        write.setHasOwnStyle(false);
        writes.add(write);
        operate.write(writes, getOutputStream(), false);
        System.out.println("文件输出正常");
    }

    /**
     * 入参数据 Map类型.多个 sheet数据写出
     *
     * @ throws Exception
     */
    @Test
    public void writeWithStyleByMapMoreSheet() throws Exception {
        List<MetaExcelWriteDTO> writes = new ArrayList<>();

        // sheet1 带自定义样式
        MetaExcelWriteDTO write = setStyle();
        getMapData(write);
        writes.add(write);

        // sheet2 取消自定义样式
        write = setStyle();
        getMapData(write);
        write.setHasOwnStyle(false);
        writes.add(write);
        operate.write(writes, getOutputStream(), true);
        System.out.println("文件输出正常");
    }

    /**
     * 获取 Map数据
     *
     * @param write
     */
    public void getMapData(MetaExcelWriteDTO write) {

        //数据处理
        List<List<Object>> param = new ArrayList<>();
        List<Object> aa = new ArrayList<>();
        aa.add(1);
        aa.add("aa");
        aa.add("bb");
        param.add(aa);
        aa = new ArrayList<>();
        aa.add(2);
        aa.add("cc");
        aa.add("dd");

        param.add(aa);
        List<List<String>> heads = new ArrayList<>();
        List<String> head = new ArrayList<>();
        head.add("你是谁");
        heads.add(head);
        head = new ArrayList<>();
        head.add("你在哪");
        heads.add(head);
        head = new ArrayList<>();
        head.add("你好呀");
        heads.add(head);
        write.setHeads(heads).setMapList(param);
    }

    /**
     * 获取对象数据
     *
     * @param write
     */
    public void get0bjData(MetaExcelWriteDTO write) {

        //编辑填充数据
        List<ExampleDtoOneDto> dataList = new ArrayList<>();
        ExampleDtoOneDto dto = new ExampleDtoOneDto();
        dto.setDocLevel("11");
        dto.setDocName("22");
        dto.setPlanDesc("aa");
        dto.setPlanType("bb");
        dto.setPlanStartTime(new Date());
        dto.setPlanEndTime(new Date());
        dto.setUm("cc");
        dataList.add(dto);
        dto = new ExampleDtoOneDto();
        dto.setDocLevel("11");
        dto.setDocName("22");
        dto.setPlanDesc("aa");
        dto.setPlanType("cc");
        dto.setPlanStartTime(new Date());
        dto.setPlanEndTime(new Date());
        dto.setUm("cc");

        dataList.add(dto);
        dto = new ExampleDtoOneDto();
        dto.setDocLevel("11");
        dto.setDocName("22");
        dto.setPlanDesc("aa");
        dto.setPlanType("bb");
        dto.setPlanStartTime(new Date());
        dto.setPlanEndTime(new Date());
        dto.setUm("cc");
        dataList.add(dto);
        write.setDataList(dataList);
        write.setParamClazz(ExampleDtoOneDto.class);
    }

    /**
     * 自定义样式
     *
     * @return
     */
    public MetaExcelWriteDTO setStyle() {
        MetaExcelWriteDTO write = new MetaExcelWriteDTO();
        //设置头行号
        write.setHeadRows(Arrays.asList(new Integer[]{0}));
        //设置自定义样式
        write.setHasOwnStyle(true);
        //头样式
        MetaCellStyle headStyle = new MetaCellStyle();
        headStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        headStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
        headStyle.setBorderBottom(BorderStyle.THIN);
        headStyle.setBorderLeft(BorderStyle.THIN);
        headStyle.setBorderRight(BorderStyle.THIN);
        headStyle.setBorderTop(BorderStyle.THIN);
        headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        headStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        MetaCellFont writeFont = new MetaCellFont();
        writeFont.setFontName("宋体");

        writeFont.setFontHeightInPoints((short) 12);
        writeFont.setColor(IndexedColors.GREEN.getIndex());
        writeFont.setBold(true);
        headStyle.setCellFont(writeFont);
        write.setHeadCellStyle(headStyle);
        //数据样式
        List<MetaDataOperate> dataOperates = new ArrayList<>();
        MetaDataOperate dataOperate = new MetaDataOperate();
        dataOperate.setColumnList(Arrays.asList(new Integer[]{0, 1}));
        dataOperate.setOperateType("SINGLE");
        Map<String, Object> check = new HashMap<>();
        check.put("key", "planType");
        check.put("value", "bb");
        check.put("basis", "EQUAL");
        dataOperate.setCheckBasis(check);
        MetaCellStyle dataStyle = new MetaCellStyle();
        dataStyle.setBorderBottom(BorderStyle.THIN);
        dataStyle.setBorderLeft(BorderStyle.THIN);
        dataStyle.setBorderRight(BorderStyle.THIN);
        dataStyle.setBorderTop(BorderStyle.THIN);
        dataStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        dataStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
        writeFont = new MetaCellFont();
        writeFont.setFontName("宋体");
        writeFont.setFontHeightInPoints((short) 11);
        writeFont.setColor(IndexedColors.BLUE.getIndex());
        dataStyle.setCellFont(writeFont);
        dataOperate.setMetaCellStyle(dataStyle);
        dataOperates.add(dataOperate);
        dataOperate = new MetaDataOperate();
        dataOperate.setColumnList(Arrays.asList(new Integer[]{2}));
        dataOperate.setOperateType("SINGLE");
        check = new HashMap<>();
        check.put("basis", "NO_CHECK");
        dataOperate.setCheckBasis(check);
        dataStyle = new MetaCellStyle();

        dataStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        dataStyle.setHexadecimalCode("#FFE698");
        dataOperate.setMetaCellStyle(dataStyle);
        dataOperates.add(dataOperate);
        write.setDataOperateList(dataOperates);
        //设置列宽
        write.setPaColumnWidth(15);
        //补充全局样式
        //MetaCellStyle globalStyle = new MetaCellStyle();
        //writeFont = new MetaCellFont();
        //writeFont.setFontName("宋体");
        //writeFont.setFontHeightInPoints(( short) 13);
        //writeFont.setColor(IndexedColors.GREY_50_PERCENT.getIndex());
        //globalstyle.setCellFont(writeFont);.
        //write.setGlobalStyle( globalstyle);
        return write;
    }

    public OutputStream getOutputStream() throws Exception {
        String filePath = "D:\\Temp\\你好呀" + System.currentTimeMillis() + ".xlsx";
        OutputStream os = Files.newOutputStream(Paths.get(filePath));
        return os;
    }

    public FileInputStream getobjTempInputStream() throws IOException {
        String excelPath = System.getProperty("file.separator") + "template" + System.getProperty("file.separator") + "objTemplate.xlsx";
        FileInputStream ins = new FileInputStream((new ClassPathResource(excelPath)).getFile());
        return ins;
    }

    public FileInputStream getMapTempInputStream() throws IOException {
        String excelPath = System.getProperty("file.separatemplate") + System.getProperty("file.separator") + "mapTemplate.xlsx";
        FileInputStream ins = new FileInputStream((new ClassPathResource(excelPath)).getFile());
        return ins;
    }
}